import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

class MyToolBar extends JToolBar {
    MyFrame frame;
    JButton toolOpen= new JButton(new ImageIcon(getClass().getResource("/Images/ButtonOpen.png")));    
    JButton toolSave= new JButton(new ImageIcon(getClass().getResource("/Images/Save.png")));
    JButton toolCopy= new JButton(new ImageIcon(getClass().getResource("/Images/Copy.png")));
    JButton toolCut=  new JButton(new ImageIcon(getClass().getResource("/Images/Cut.png")));
    JButton toolPaste=new JButton(new ImageIcon(getClass().getResource("/Images/Paste.png")));
    JButton toolPrint=new JButton(new ImageIcon(getClass().getResource("/Images/Print.png")));
    JButton toolUndo= new JButton(new ImageIcon(getClass().getResource("/Images/Undo.png")));
    JButton toolRedo= new JButton(new ImageIcon(getClass().getResource("/Images/Redo.png")));
    JButton toolPrintPreview=new JButton(new ImageIcon(getClass().getResource("/Images/PrintPreview.png")));
    JToggleButton toolToolbox= new JToggleButton(new ImageIcon(getClass().getResource("/Images/ToolBox.png")));
    JButton toolProperties= new JButton(new ImageIcon(getClass().getResource("/Images/Properties.png")));
    
    MyToolBar(MyFrame frame) {
        this.frame=frame;
        toolSave.setEnabled(false);
        toolCopy.setEnabled(false);
        toolCut.setEnabled(false);
        if (MainWindow.copiedComponents.isEmpty()) {
            toolPaste.setEnabled(false);
        }
        toolToolbox.setSelected(true);
        toolUndo.setEnabled(false);
        toolRedo.setEnabled(false);
        //super();
        //*************** Adjust The Tool Bar *******************************//
        //this.setLayout(new FlowLayout());
        //this.setSize(50,700);
        //*************** Setting ToolTips ***********************************//
        toolOpen.setToolTipText("Open View");
        toolSave.setToolTipText("Save");
        toolCopy.setToolTipText("Copy");
        toolCut.setToolTipText("Cut");
        toolPaste.setToolTipText("Paste");
        toolPrint.setToolTipText("Print");
        toolUndo.setToolTipText("Undo");
        toolRedo.setToolTipText("Redo");
        toolPrintPreview.setToolTipText("Print Preview");
        toolToolbox.setToolTipText("ToolBox");
        toolProperties.setToolTipText("Properties");
        //***************Adding Action Listeners To The Buttons***************//
        ToolBarHandler toolBarHandler = new ToolBarHandler();
        toolOpen.addActionListener(toolBarHandler);
        toolSave.addActionListener(toolBarHandler);
        toolCopy.addActionListener(toolBarHandler);
        toolPaste.addActionListener(toolBarHandler);
        toolToolbox.addActionListener(toolBarHandler);
        toolCut.addActionListener(toolBarHandler);
        toolUndo.addActionListener(toolBarHandler);
        toolRedo.addActionListener(toolBarHandler);
        toolProperties.addActionListener(toolBarHandler);
        //*************** Adding Buttons To The Bar **************************//
        this.add(toolOpen);
        this.addSeparator();
        this.add(toolSave);
        this.addSeparator();
        this.add(toolCopy);
        this.add(toolCut);
        this.addSeparator();
        this.add(toolPaste);
        this.addSeparator();
        this.add(toolUndo);
        this.add(toolRedo);
        this.addSeparator();
        this.add(toolPrint);
        this.add(toolPrintPreview);
        this.addSeparator();
        this.add(toolToolbox);
        this.add(toolProperties);
    }
    
    public class ToolBarHandler implements ActionListener {
        public void actionPerformed(ActionEvent a) {
            AbstractButton button = (AbstractButton)a.getSource();
            if (button.isEnabled()) {
                if (button.getToolTipText().equals("Save")) {
                    if (frame.flagNewDesign) {
                        String formName=JOptionPane.showInputDialog("Enter Form's Name:",frame.getTitle());
                        if (formName!=null) {
                            boolean flagNameExists=false;
                            for (String str : MainWindow.myForms) {
                                if (formName.equals(str)) {
                                    flagNameExists=true;
                                }
                            }
                            if (flagNameExists) {
                                JOptionPane.showMessageDialog(frame,"A Form With The Name You Specified Already Exists!\nPlease Choose Another Name");
                            } else if (formName.equals("")) {
                                JOptionPane.showMessageDialog(frame,"Form Name Cannot Be Blank!");
                            } else {
                                Save.save(formName,frame.panel,frame.componentList,frame.getTableName());
                                MainWindow.refreshFormList(formName);
                                frame.flagDirty=false;
                                button.setEnabled(false);
                            }
                        }
                        frame.panel.requestFocusInWindow();
                    } else {
                        Save.save(frame.getTitle(),frame.panel,frame.componentList,frame.getTableName());
                        frame.flagDirty=false;
                        button.setEnabled(false);
                    }
                } else if (button.getToolTipText().equals("Copy")) {
                    copy();
                } else if (button.getToolTipText().equals("Cut")) {
                    cut();
                } else if (button.getToolTipText().equals("Paste")) {
                    paste();
                } else if (button.getToolTipText().equals("ToolBox")) {
                    frame.toolBoxMode= ! frame.toolBoxMode;
                    frame.toolBox2Item.setSelected(frame.toolBoxMode);
                    frame.toolBoxItem.setSelected(frame.toolBoxMode);
                    toolToolbox.setSelected(frame.toolBoxMode);
                    
                    if (frame.toolBoxMode) {
                        frame.add(frame.toolbox, BorderLayout.WEST);
                        frame.validate();
                        frame.repaint();
                    } else {
                        frame.remove(frame.toolbox);
                        frame.validate();
                        frame.repaint();
                    }
                    frame.panel.requestFocusInWindow();
                } else if (button.getToolTipText().equals("Undo")) {
                    undo();
                } else if (button.getToolTipText().equals("Redo")) {
                    redo();
                } else if (button.getToolTipText().equals("Properties")) {
                         /* Get the component that is
                         responsable for the action */
                    for (MyComponentContainer component : frame.componentList) {
                        if (component.isChosen()) {
                            MyPropertiesWindow properties = new MyPropertiesWindow(component,frame);
                        }
                    }
                } else if (button.getToolTipText().equals("Open View")) {
                    OutputPanel outputPanel = new OutputPanel(MainWindow.dbCon,frame.componentList, frame.panel.getBackground(), frame.panel.getSize(), MainWindow.recordsLimit);
                    MainPanel mainPanel = new MainPanel (outputPanel.compPanel);
                    outputPanel.compPanel.setLocation(0,0);
                    mainPanel.drawPanel.add(outputPanel.compPanel);
                    mainPanel.insertRecordNavigationMenu (outputPanel);
                    OutputFrame outputFrame = new OutputFrame(mainPanel);
                    outputFrame.refreshScrolls();
                    outputFrame.setTitle(frame.getTitle());
                    outputFrame.setVisible(true);
                    if (frame.flagDirty) {
                        Save.save(frame.getTitle(),frame.panel,frame.componentList,frame.getTableName());
                    }
                    frame.dispose();
                }
            }
        }
    }
    public void refreshPasteButton() {
        if (MainWindow.copiedComponents.isEmpty()==false) {
            toolPaste.setEnabled(true);
            frame.pasteItem.setEnabled(true);
        }
    }
    
    public MyComponentContainer getCopiedComponentWithMinX() {
        MyComponentContainer myComponent=MainWindow.copiedComponents.get(0);
        for (MyComponentContainer component : MainWindow.copiedComponents) {
            if (component.getComponent().getX() < myComponent.getComponent().getX()) {
                myComponent=component;
            }
        }
        return myComponent;
    }
    
    public MyComponentContainer getCopiedComponentWithMinY() {
        MyComponentContainer myComponent=MainWindow.copiedComponents.get(0);
        for (MyComponentContainer component : MainWindow.copiedComponents) {
            if (component.getComponent().getY() < myComponent.getComponent().getY()) {
                myComponent=component;
            }
        }
        return myComponent;
    }
    
    public void paste() {
        int minX = getCopiedComponentWithMinX().getComponent().getX();
        int minY = getCopiedComponentWithMinY().getComponent().getY();
        if (MainWindow.flagCut) {
            frame.removeComponentSelection();
            for (MyComponentContainer component : MainWindow.copiedComponents) {
                incrementNamesCount(component,component);
                component.getComponent().setSize(component.getComponent().getSize());
                component.getComponent().setLocation(component.getComponent().getX()+5-minX,component.getComponent().getY()+5-minY);
                component.setFrame(frame);
                frame.panel.add(component.getComponent());
                frame.componentList.add(component);
                frame.formatToolBar.refreshObjects();
                if (MainWindow.copiedComponents.size()==1) {
                    frame.formatToolBar.setSelectedComponent(component.getName());
                } else {
                    frame.formatToolBar.setSelectedComponent(null);
                }
                component.setChosen(true);
                frame.selectedComponentList.add(component.getName());
                component.showBorder();
                frame.duplicateItem.setEnabled(true);
                frame.deleteItem.setEnabled(true);
                frame.copyItem.setEnabled(true);
                frame.cutItem.setEnabled(true);
                frame.toolbar.toolCopy.setEnabled(true);
                frame.toolbar.toolCut.setEnabled(true);
                frame.panel.validate();
                frame.panel.repaint();
                frame.flagDirty=true;
                toolSave.setEnabled(true);
                frame.saveItem.setEnabled(true);
                component.resizePanel();
                frame.formatToolBar.enableButtons(component);
                frame.refreshScrolls();
            }
            MainWindow.copiedComponents.clear();
            toolPaste.setEnabled(false);
            frame.handleUndo();
        } else {
            frame.removeComponentSelection();
            MyComponentContainer component=null;
            for (MyComponentContainer c : MainWindow.copiedComponents) {
                component=ComponentClone.clone(c);
                incrementNamesCount(c,component);
                component.getComponent().setLocation(c.getComponent().getX()+5-minX,c.getComponent().getY()+5-minY);
                component.setFrame(frame);
                frame.panel.add(component.getComponent());
                frame.componentList.add(component);
                frame.formatToolBar.refreshObjects();
                if (MainWindow.copiedComponents.size()==1) {
                    frame.formatToolBar.setSelectedComponent(component.getName());
                } else {
                    frame.formatToolBar.setSelectedComponent(null);
                }
                component.setChosen(true);
                frame.selectedComponentList.add(component.getName());
                component.showBorder();
                frame.duplicateItem.setEnabled(true);
                frame.deleteItem.setEnabled(true);
                frame.copyItem.setEnabled(true);
                frame.cutItem.setEnabled(true);
                frame.toolbar.toolCopy.setEnabled(true);
                frame.toolbar.toolCut.setEnabled(true);
                frame.panel.validate();
                frame.panel.repaint();
                frame.flagDirty=true;
                toolSave.setEnabled(true);
                frame.saveItem.setEnabled(true);
                component.resizePanel();
                frame.formatToolBar.enableButtons(component);
                frame.refreshScrolls();
            }
            frame.handleUndo();
        }
        frame.panel.requestFocusInWindow();
    }
    
    public void cut() {
        if (MainWindow.copiedComponents.isEmpty()==false) {
            MainWindow.copiedComponents.clear();
        }
        ArrayList<MyComponentContainer> deletedComponents = new ArrayList<MyComponentContainer>();
        for (MyComponentContainer c : frame.componentList){
            if (c.isChosen()){
                c.hideBorder();
                c.setChosen(false);
                MainWindow.copiedComponents.add(c);
                frame.panel.remove(c.getComponent());
                frame.selectedComponentList.remove(c.getName());
                deletedComponents.add(c);
                frame.flagDirty=true;
                toolSave.setEnabled(true);
                frame.saveItem.setEnabled(true);
                toolPaste.setEnabled(true);
                frame.pasteItem.setEnabled(true);
                MainWindow.flagCut=true;
            }
        }
        for (MyComponentContainer cDelete : deletedComponents){
            frame.componentList.remove(cDelete);
        }
        frame.formatToolBar.refreshObjects();
        frame.formatToolBar.disableButtons();
        frame.duplicateItem.setEnabled(false);
        frame.deleteItem.setEnabled(false);
        frame.copyItem.setEnabled(false);
        frame.cutItem.setEnabled(false);
        frame.toolbar.toolCopy.setEnabled(false);
        frame.toolbar.toolCut.setEnabled(false);
        frame.handleUndo();
        frame.panel.validate();
        frame.panel.repaint();
        frame.panel.requestFocusInWindow();
    }
    
    public void copy() {
        if (MainWindow.copiedComponents.isEmpty()==false) {
            MainWindow.copiedComponents.clear();
        }
        for (MyComponentContainer c : frame.componentList) {
            if (c.isChosen()) {
                MainWindow.copiedComponents.add(c);
                toolPaste.setEnabled(true);
                frame.pasteItem.setEnabled(true);
                MainWindow.flagCut=false;
            }
        }
        frame.panel.requestFocusInWindow();
    }
    
    public void undo() {
        toolRedo.setEnabled(true);
        frame.redoItem.setEnabled(true);
        for (MyComponentContainer c : frame.componentList) {
            if (c.isChosen()) {
                c.hideBorder();
            }
            frame.panel.remove(c.getComponent());
        }
        frame.undoIndex--;
        UndoHistoryInformation undoHistoryInfo = frame.undoHistory.get(frame.undoIndex);
        frame.panel.setSize(undoHistoryInfo.panelDimension);
        frame.panel.setBackground(undoHistoryInfo.panelBackground);
        ArrayList<MyComponentContainer> undo = undoHistoryInfo.componentList;
        if (frame.undoIndex<=0) {
            toolUndo.setEnabled(false);
            frame.undoItem.setEnabled(false);
        }
        if (frame.componentList.isEmpty()==false) {
            frame.componentList.clear();
        }
        for (MyComponentContainer c : undo) {
            c.setChosen(false);
            MyComponentContainer component = ComponentClone.clone(c);
            frame.panel.add(component.getComponent());
            frame.componentList.add(component);
        }
        frame.selectedComponentList.clear();
        frame.formatToolBar.refreshObjects();
        frame.formatToolBar.disableButtons();
        frame.formatToolBar.setSelectedComponent("Panel");
        frame.duplicateItem.setEnabled(false);
        frame.deleteItem.setEnabled(false);
        frame.copyItem.setEnabled(false);
        frame.cutItem.setEnabled(false);
        frame.toolbar.toolCopy.setEnabled(false);
        frame.toolbar.toolCut.setEnabled(false);
        frame.flagDirty=true;
        frame.panel.repaint();
        frame.panel.requestFocusInWindow();
    }
    
    public void redo() {
        toolUndo.setEnabled(true);
        frame.undoItem.setEnabled(true);
        for (MyComponentContainer c : frame.componentList) {
            if (c.isChosen()) {
                c.hideBorder();
            }
            frame.panel.remove(c.getComponent());
        }
        frame.undoIndex++;
        UndoHistoryInformation undoHistoryInfo = frame.undoHistory.get(frame.undoIndex);
        frame.panel.setSize(undoHistoryInfo.panelDimension);
        frame.panel.setBackground(undoHistoryInfo.panelBackground);
        ArrayList<MyComponentContainer> redo = undoHistoryInfo.componentList;
        if (frame.undoIndex>=frame.undoHistory.size()-1) {
            toolRedo.setEnabled(false);
            frame.redoItem.setEnabled(false);
        }
        if (frame.componentList.isEmpty()==false) {
            frame.componentList.clear();
        }
        for (MyComponentContainer c : redo) {
            c.setChosen(false);
            MyComponentContainer component = ComponentClone.clone(c);
            frame.panel.add(component.getComponent());
            frame.componentList.add(component);
        }
        frame.selectedComponentList.clear();
        frame.formatToolBar.refreshObjects();
        frame.formatToolBar.disableButtons();
        frame.formatToolBar.setSelectedComponent("Panel");
        frame.duplicateItem.setEnabled(false);
        frame.deleteItem.setEnabled(false);
        frame.copyItem.setEnabled(false);
        frame.cutItem.setEnabled(false);
        frame.toolbar.toolCopy.setEnabled(false);
        frame.toolbar.toolCut.setEnabled(false);
        frame.flagDirty=true;
        frame.panel.repaint();
        frame.panel.requestFocusInWindow();
    }
    
    public void incrementNamesCount(MyComponentContainer c, MyComponentContainer component) {
        if (c instanceof MyLabelContainer) {
            frame.namesCount[MyToolBox.LABEL]++;
            component.getComponent().setName("Label"+frame.namesCount[MyToolBox.LABEL]);
        } else if (c instanceof MyTextFieldContainer) {
            frame.namesCount[MyToolBox.TEXTFIELD]++;
            component.getComponent().setName("Textbox"+frame.namesCount[MyToolBox.TEXTFIELD]);
        } else if (c instanceof MyButtonContainer) {
            frame.namesCount[MyToolBox.BUTTON]++;
            component.getComponent().setName("Button"+frame.namesCount[MyToolBox.BUTTON]);
        } else if (c instanceof MyToggleButtonContainer) {
            frame.namesCount[MyToolBox.TOGGLEBUTTON]++;
            component.getComponent().setName("ToggleButton"+frame.namesCount[MyToolBox.TOGGLEBUTTON]);
        } else if (c instanceof MyPictureContainer) {
            frame.namesCount[MyToolBox.PICTURE]++;
            component.getComponent().setName("Picture"+frame.namesCount[MyToolBox.PICTURE]);
        } else if (c instanceof MyCheckBoxContainer) {
            frame.namesCount[MyToolBox.CHECKBOX]++;
            component.getComponent().setName("CheckBox"+frame.namesCount[MyToolBox.CHECKBOX]);
        } else if (c instanceof MyRadioButtonContainer) {
            frame.namesCount[MyToolBox.RADIOBUTTON]++;
            component.getComponent().setName("RadioButton"+frame.namesCount[MyToolBox.RADIOBUTTON]);
        } else if (c instanceof MyComboBoxContainer) {
            frame.namesCount[MyToolBox.COMBOBOX]++;
            component.getComponent().setName("Combobox"+frame.namesCount[MyToolBox.COMBOBOX]);
        } else if (c instanceof MyListContainer) {
            frame.namesCount[MyToolBox.LIST]++;
            component.getComponent().setName("Listbox"+frame.namesCount[MyToolBox.LIST]);
        }
    }
}
